//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "Turkey.h"
using namespace Cephei::QL::Times::Calendars;
#include <gen/QL/Times/Period.h>
#include <gen/QL/Times/Calendar.h>
using namespace Cephei::QL::Times;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Times::Calendars::CTurkey::CTurkey () : CCalendar(CTurkey::typeid)
{
    try
    {
#ifdef HANDLE
        _phTurkey = NULL;
#endif
        _ppTurkey = new boost::shared_ptr<QuantLib::Turkey> (new QuantLib::Turkey ( ));
        SetCalendar (boost::dynamic_pointer_cast<QuantLib::Calendar> (*_ppTurkey));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::Calendars::CTurkey::CTurkey (boost::shared_ptr<QuantLib::Turkey>& childNative, Object^ owner) : CCalendar(CTurkey::typeid)
{
#ifdef HANDLE
	_phTurkey = NULL;
#endif
	_ppTurkey = &childNative;
    _ppCalendar = new boost::shared_ptr<QuantLib::Calendar> (boost::dynamic_pointer_cast<QuantLib::Calendar> (*_ppTurkey));
}
Cephei::QL::Times::Calendars::CTurkey::CTurkey (QuantLib::Turkey& childNative, Object^ owner) : CCalendar(CTurkey::typeid)
{
#ifdef HANDLE
	_phTurkey = NULL;
#endif
	_ppTurkey = new boost::shared_ptr<QuantLib::Turkey> (&childNative);
    _ppCalendar = new boost::shared_ptr<QuantLib::Calendar> (boost::dynamic_pointer_cast<QuantLib::Calendar> (*_ppTurkey));
    _TurkeyOwner = owner;
    _CalendarOwner = owner;
}

Cephei::QL::Times::Calendars::CTurkey::CTurkey (CTurkey^ copy) : CCalendar(CTurkey::typeid)
{
#ifdef HANDLE
	_phTurkey = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppTurkey = new boost::shared_ptr<QuantLib::Turkey> (copy->GetShared());
        _ppCalendar = new boost::shared_ptr<QuantLib::Calendar> (boost::dynamic_pointer_cast<QuantLib::Calendar> (*_ppTurkey));
    }
}
Cephei::QL::Times::Calendars::CTurkey::CTurkey (System::Type^ t) : CCalendar(CTurkey::typeid)
{
#ifdef HANDLE
	_phTurkey = NULL;
#endif
	if (!t->IsSubclassOf(CTurkey::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Times::Calendars::CTurkey::CTurkey (QuantLib::Handle<QuantLib::Turkey>& childNative, Object^ owner)  : CCalendar(CTurkey::typeid)
{
	_phTurkey = &childNative;
	_ppTurkey = &static_cast<boost::shared_ptr<QuantLib::Turkey>>(childNative.currentLink());
    _ppCalendar = new boost::shared_ptr<QuantLib::Calendar> (boost::dynamic_pointer_cast<QuantLib::Calendar> (*_ppTurkey));
    _TurkeyOwner = owner;
}
Cephei::QL::Times::Calendars::CTurkey::CTurkey (QuantLib::Handle<QuantLib::Turkey> childNative)  : CCalendar(CTurkey::typeid)
{
	_phTurkey = &childNative;
	_ppTurkey = &static_cast<boost::shared_ptr<QuantLib::Turkey>>(childNative.currentLink());
    _ppCalendar = new boost::shared_ptr<QuantLib::Calendar> (boost::dynamic_pointer_cast<QuantLib::Calendar> (*_ppTurkey));
}
#endif
#ifdef STRUCT
Cephei::QL::Times::Calendars::CTurkey::CTurkey (QuantLib::Turkey childNative)  : CCalendar(CTurkey::typeid)
{
#ifdef HANDLE
	_phTurkey = NULL;
#endif
	_ppTurkey = new boost::shared_ptr<QuantLib::Turkey> (new QuantLib::Turkey (childNative));
    _ppCalendar = new boost::shared_ptr<QuantLib::Calendar> (boost::dynamic_pointer_cast<QuantLib::Calendar> (*_ppTurkey));
}
#endif

Cephei::QL::Times::Calendars::CTurkey::~CTurkey ()
{
    if (_ppTurkey != NULL)
    {
	    delete _ppTurkey;
        _ppTurkey = NULL;
    }
}
Cephei::QL::Times::Calendars::CTurkey::!CTurkey ()
{
    if (_ppTurkey != NULL)
    {
	    delete _ppTurkey;
    }
}
QuantLib::Turkey& Cephei::QL::Times::Calendars::CTurkey::GetReference ()
{
    if (_ppTurkey == NULL) throw gcnew NativeNullException ();
	return **_ppTurkey;
}
boost::shared_ptr<QuantLib::Turkey>& Cephei::QL::Times::Calendars::CTurkey::GetShared ()
{
    if (_ppTurkey == NULL) throw gcnew NativeNullException ();
	return *_ppTurkey;
}
QuantLib::Turkey* Cephei::QL::Times::Calendars::CTurkey::GetPointer ()
{
    if (_ppTurkey == NULL) throw gcnew NativeNullException ();
	return &**_ppTurkey;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Turkey>& Cephei::QL::Times::Calendars::CTurkey::GetHandle ()
{
	if (_phTurkey == NULL)
	{
		_phTurkey = new Handle<QuantLib::Turkey> (*_ppTurkey);
	}
	return *_phTurkey;
}
#endif
bool Cephei::QL::Times::Calendars::CTurkey::HasNative () 
{
	return (_ppTurkey != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Times::Calendars::ITurkey^ Cephei::QL::Times::Calendars::CTurkey_Factory::Create ()
{
    return gcnew CTurkey ();
}
